package com.renbang.test.quartz.da1;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Scanner;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.CronTrigger; 

public class SimpleExample {


	public void start() throws Exception {
		// 通过SchedulerFactory获取一个调度器实例
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		// 通过过JobDetail封装HelloJob，同时指定Job在Scheduler中所属组及名称，这里，组名为group1，而名称为job1。
		JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1")
				.build();
		
		
//		job.getJobDataMap().put("CRAWLER", task);
		// 创建一个SimpleTrigger实例，指定该Trigger在Scheduler中所属组及名称。
		// 接着设置调度的时间规则.当前时间运行
		Trigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(
				CronScheduleBuilder
				.cronSchedule("0/3 * * * * ?")).build();
//				.startAt(runTime).build();
//		sched.triggerJob(job.getKey());
		// 注册并进行调度
		sched.scheduleJob(job, trigger);
		sched.resumeTrigger(trigger.getKey());//重启触发器  
		// 启动调度器
		sched.start();
//		sched.addJob(job, false);
		String order = new Scanner(System.in).next();
		if(order.equals("pause")){
			System.out.println("shabi傻逼");
			sched.pauseJob(job.getKey());
		}else if(order.equals("resume")){
			sched.resumeJob(job.getKey());
		}else if(order.equals("isStart")){
			System.out.println(sched.isStarted());;
		}else if(order.equals("shutdown")){
			sched.shutdown();  
		}
//		sched.deleteJob(job.getKey());
	}

	public static void main(String[] args) throws Exception {
		SimpleExample example = new SimpleExample();
		example.start();
	}
}